<?php


namespace App\Logic\Account\Form;

use App\Core\BaseListForm;
use App\Core\Exception\DefaultException;
use App\Model\Account\RechargeModel;
use App\Model\Member\MemberModel;
use App\Model\UserModel;
use App\Util\CommonHelper;
use App\Util\UserIdentityHelper;
use EasySwoole\Mysqli\QueryBuilder;

class RechargeListForm extends BaseListForm
{
    public $id;
    public $user_id;
    public $username;
    public $amout;
    public $score;
    public $recharge_state;
    public $payment_type;
    public $payment_state;
    public $payment_content;
    public $created_at;
    public $is_admin = false;

    /**
     * 充值列表
     * @return \App\Core\Output
     * @throws DefaultException
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function lists()
    {
        $where = [];
        if(UserIdentityHelper::getType() == UserModel::TYPE_INSIDE) {
            $this->is_admin = true;
            /**后台管理员*/
            if ($this->checkField($this->user_id)) {
                $where['user_id'] = $this->user_id;
            }
            if ($this->checkField($this->username)) {
                $subBuilder = QueryBuilder::subQuery();
                $subBuilder->where('username', $this->username);
                $subBuilder->get(MemberModel::create()->tableName(), null, "id");
                $where['user_id'] = [$subBuilder, 'in'];
            }
        }else{
            $this->user_id = UserIdentityHelper::getId();
            $where['user_id'] = $this->user_id;
        }

        if ($this->checkField($this->id)) {
            $where['id'] = $this->id;
        }
        if ($this->checkField($this->recharge_state)) {
            $where['recharge_state'] = $this->recharge_state;
        }
        if ($this->checkField($this->payment_type)) {
            $where['payment_type'] = $this->payment_type;
        }
        if ($this->checkField($this->payment_state)) {
            $where['payment_state'] = $this->payment_state;
        }
        if ($this->checkField($this->payment_content)) {
            $where['payment_content'] = $this->payment_content;
        }
        if ($this->checkField($this->created_at) && strtotime($this->created_at) > 0) {
            $this->created_at = date('Y-m-d', strtotime($this->created_at));
            $where['created_at'] = [[$this->created_at . ' 00:00:00', $this->created_at . ' 23:59:59'], 'between'];
        }

        $model = RechargeModel::makeQueryModel($where, [], $this->getPagination());
        /** @see RechargeModel::member()*/
        $model->with(['member']);
        $model->order( 'id', 'DESC');

        $modelResult = $model->all();

        $list = [];
        foreach($modelResult as $v) {
            $item   = $v->toArray();
            $item['username']           = $v->member->username ?? '';
            $item['recharge_state_label'] = RechargeModel::getRechargeStateDict($item['recharge_state']);
            $item['payment_type_label'] = RechargeModel::getPaymentTypeDict($item['payment_type']);
            $item['payment_state_label'] = RechargeModel::getPaymentStateDict($item['payment_state']);
            /**判断payment_content是否是一个图片链接*/
            $imageExtensions = ['.jpg', '.jpeg', '.png', '.gif']; // 常见图片文件扩展名
            $extension = strtolower(substr($item['payment_content'], strrpos($item['payment_content'], '.'))); // 获取链接的文件扩展名
            $item['payment_content_is_img'] = in_array($extension, $imageExtensions);

            /**不是后台管理员*/
            if (!$this->is_admin) {
                unset($item['created_by'], $item['updated_at'], $item['updated_by'], $item['is_delete'], $item['deleted_at']);
            }
            $list[] = $item;
        }

        $total = $model->lastQueryResult()->getTotalCount();
        $pagination = $this->getPagination();
        $pagination['total_count'] = $total;

        $result = [
            'list'       => $list,
            'pagination' => $pagination
        ];
        return $result;
    }

}
